package com.guan.study.easyExcelDemo.write.write02_合并单元格写;

import com.alibaba.excel.EasyExcel;
import com.guan.study.easyExcelDemo.Student;
import com.guan.study.easyExcelDemo.Teacher;
import com.guan.study.easyExcelDemo.utils.CustomConverter;
import com.guan.study.easyExcelDemo.utils.CustomMergeColumn;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Date: 2023/7/2 10:20
 * @Author: yaobai
 * @Description: 比较复杂的写，合单元格的导出
 */
public class WriteExcel02 {

    private static final String PATH = "E:\\WorkDirTest2\\";

    public static void main(String[] args) {

        //告知哪些列需要合并
        List<String> megerColumn = new ArrayList<>();
        //其中：学院、资历、头像、头衔、简介、是否删除都是有重复的，可以合并, 现规定以下三列合并
        megerColumn.add("school");
        megerColumn.add("level");
        megerColumn.add("career");

        String fileName2 = PATH + "教师统计表.xlsx";
        EasyExcel.write(fileName2, Teacher.class)
                //无参，就是默认sheet名， 也可传入”sheet1“
                .sheet()
                //设置表头信息
                .head(Teacher.class)
                //注册自定义策略，设置可以合并的列
                .registerWriteHandler(new CustomMergeColumn(megerColumn))
                //写入数据
                .doWrite(data2());

        System.out.println("写入完毕");

    }

    /**
     * @date: 2023/7/20 15:27
     * @author: yaobai
     * @description: 模拟数据库查询到的数据
     * @param:
     * @return:
     **/
    private static List<Teacher> data2(){

        List<Teacher> list = new ArrayList<>();

        for (int i = 0; i < 20; i++) {
            Teacher tea = new Teacher();
            if (i<8){
                tea.setSchool("0");
                tea.setCareer("北京大学资深教师");
                tea.setAvatar("头像A");
                tea.setLevel(1);
                tea.setIsDeleted(false);
            }else if (i<12){
                tea.setSchool("1");
                tea.setAvatar("头像B");
                tea.setLevel(1);
                tea.setIsDeleted(false);
                tea.setCareer("清华大学资深教师");
            } else{
                tea.setSchool("2");
                tea.setAvatar("头像C");
                tea.setLevel(2);
                tea.setCareer("武汉大学计算机系教授");
                tea.setIsDeleted(true);
            }
            tea.setName("张三"+i);
            tea.setIntro("教师简介12456789");
            tea.setSort(i);
            tea.setGmtCreate(new Date());
            tea.setGmtModified(new Date());
            tea.setWorkNo("100"+i);

            list.add(tea);
        }

        return list;
    }

}
